import getEtag from '@/tools/qetag';
import defaultConfig from '../../config/defaultConfig';
import { createFromIconfontCN } from '@ant-design/icons';


/**
 * 通过文件名获取后缀
 * @param fileName
 */
export const getFileSuffix = (fileName)=>{
  let startIndex = fileName.lastIndexOf(".");
  if(startIndex != -1){
    return  fileName.substring(startIndex+1, fileName.length).toLowerCase();
  }
  return false;
}

/**
 * 去掉后缀，只返回文件名
 * @param fileName
 */
export const getFileName = (fileName)=>{
  let startIndex = fileName.lastIndexOf(".");
  if(startIndex != -1){
    return  fileName.substring(0,startIndex).toLowerCase();
  }
  return false;
}


/**
 * 获取文件Hash
 * @param file
 */
export const fileHash = (file)=>{    return new Promise(
  function(resolve, reject) {
    let reader = new FileReader()
    reader.readAsArrayBuffer(file)
    reader.onload = function() {
      resolve( getEtag(this.result))
    }
  })
}



/**
 * 获取视频上传地址
 */
export const getFileHost = (fileSuffix)=>{
  return defaultConfig.filehost;
  // return new Promise(function (resolve, reject) {
  //   console.log(file)
  //   // return defaultConfig.filehost;
  //       resolve(defaultConfig.filehost);
  //
  //       // reject('timeout in ' + timeOut + ' seconds.');
  //
  // }).then(function (r) {
  //
  // }).catch(function (reason) {
  // });

}

// /**
//  * 获取文件播放时 处理后缀
//  * @param fileType
//  */
// export const getFileTargetType = (fileType)=>{
//     //
//     if(fileType == 'mp4' || fileType == 'MP4'){
//       return 'video'
//     }
//     let fileTypeList = ['ppt','doc','pdf','psd'];
//     if(fileTypeList.includes(fileType)){
//       fileTypeList.includes(fileType)
//     }
//     if(fileType == 'pptx'){
//         return 'ppt'
//     }
//     if(fileType == 'docx'){
//       return 'doc'
//     }
//     return 'download'
// }

/**
 * 获取taskIcon
 * @param targetType
 */
export const getTaskIcon = (targetType,fileType)=>{
  let temp = '';
  if(targetType == 'resource_file'){
    if(fileType == 'mp4'){
      temp = 'iconicon_video';
    }else if(fileType == 'ppt'){
      temp='iconppt'
    }else if(fileType == 'pptx'){
      temp='iconppt'
    }else if(fileType == 'psd'){
      temp='iconadobe-photoshop'
    }else if(fileType == 'doc'){
      temp='icondoc'
    }else if(fileType == 'docx'){
      temp='icondoc'
    }else if(fileType == 'zip'){
      temp='iconzip'
    }else if(fileType == 'exe'){
      temp='iconexe'
    }else if(fileType == 'dmg'){
      temp='icondmg'
    }else if(fileType == 'pdf'){
      temp='iconpdf'
    }else{
      temp = 'iconwenjian2'
    }
  }else if(targetType == 'homework'){
    if(fileType == 'homework'){
      temp = 'iconzuoye'
    }else{
      temp = 'iconzuoye1'
    }
  }else if(targetType == 'exam'){
      temp = 'iconshijuan'
  }else if(targetType == 'live'){
    temp = 'iconzhibo2'
  }else{
    temp = 'iconwenjian2'
  }
  return temp
}

/**
 * 获取TargetCount
 * @param targetType
 */
export const chapterTaskCount = (targetType,fileType)=>{
  if(targetType == 'resource_file'){
    if(fileType == 'mp4'){
      return 1;
    }else{
      return 2;
    }
  }else if(targetType == 'homework'){
    if(fileType == 'homework'){
      return 3;
    }else{
      return 4;
    }
  }else if(targetType == 'exam'){
    return 5;
  }else{
    return 2;
  }
}

export const getVideoLen = (file) => {

    var url = URL.createObjectURL(file);//获取录音时长
    var audioElement = new Audio(url);//audio也可获取视频的时长
    var length = 0;
    audioElement.addEventListener("loadedmetadata",  (_event)=>{
        return parseInt(audioElement.duration);
    });


}
